package com.zmn.plat.services.impl.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.user.UserDevice;
import com.zmn.plat.model.entity.user.UserDeviceQuery;
import com.zmn.plat.persistence.interfaces.user.UserDeviceDao;
import com.zmn.plat.services.interfaces.user.UserDeviceService;

/**
 * @author nowind
 * @since 2017/7/31 14:52
 */
@Service
public class UserDeviceServiceImpl implements UserDeviceService {

    @Resource
    private UserDeviceDao userDeviceDao;

    @Override
    public UserDevice findByKey(Integer deviceId) {
        return userDeviceDao.findByKey(deviceId);
    }

    @Override
    public UserDevice findByQuery(String deviceCode) {
        return this.findByQuery(0, deviceCode);
    }

    @Override
    public UserDevice findByQuery(Integer userId, String deviceCode) {
        UserDeviceQuery query = new UserDeviceQuery();
        query.setUserId(userId);
        query.setDeviceCode(deviceCode);
        return this.findByQuery(query);
    }

    @Override
    public UserDevice findByQuery(UserDeviceQuery query) {
        return userDeviceDao.findByQuery(query);
    }

    @Override
    public List<UserDevice> listPageByQuery(UserDeviceQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : userDeviceDao.listPageByQuery(query);
    }

    @Override
    public List<UserDevice> listByQuery(UserDeviceQuery query) {
        return userDeviceDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(UserDeviceQuery query) {
        return userDeviceDao.countByQuery(query);
    }

    @Override
    public Integer updateStatus(Integer deviceId, Integer status) {
        UserDevice userDevice = new UserDevice();
        userDevice.setDeviceId(deviceId);
        userDevice.setStatus(status);
        return this.updateForLogin(userDevice);
    }

    @Override
    public Integer updateStatus(UserDevice userDevice) {
        return userDeviceDao.updateStatus(userDevice);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> deviceIds, Integer status) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(deviceIds);
        dto.setStatus(status);
        return this.updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        return userDeviceDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateForLogin(Integer deviceId, String lastLoginIp, String lastLoginLatlng) {
        UserDevice userDevice = new UserDevice();
        userDevice.setDeviceId(deviceId);
        userDevice.setLastLoginIp(lastLoginIp);
        return this.updateForLogin(userDevice);
    }

    @Override
    public Integer updateForLogin(UserDevice userDevice) {
        if (userDevice.getLastLoginTime() == null) {
            userDevice.setLastLoginTime(DateUtil.getNow());
        }
        return userDeviceDao.updateForLogin(userDevice);
    }

    @Override
    public Integer insert(UserDevice userDevice) {
        if (userDevice.getRegistTime() == null) {
            userDevice.setRegistTime(DateUtil.getNow());
        }
        return userDeviceDao.insert(userDevice);
    }

    @Override
    public Integer deleteByKey(Integer deviceId) {
        return userDeviceDao.deleteByKey(deviceId);
    }
}
